package com.ggs.rg.ybjb.adminmodule.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.adminmodule.service.MaterService;
import com.ggs.rg.ybjb.adminmodule.service.TCommentService;
import com.ggs.rg.ybjb.adminmodule.util.ImageUtil;
import com.ggs.rg.ybjb.adminmodule.util.ListPagesUitl;
import com.ggs.rg.ybjb.pojomodule.pojo.Mater;
import com.ggs.rg.ybjb.pojomodule.pojo.TComment;
import com.ggs.rg.ybjb.pojomodule.util.CommonUtil;
import com.ggs.rg.ybjb.pojomodule.util.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cxw
 * @since 2021-11-04
 */
/*
    查看评论  管理员和用户查看所有评论
    添加品论   用户发布评论   管理员回复评论
    删除评论  管理员删除评论
 */

@RestController
@RequestMapping("/commentModule")
public class TCommentController {


    @Autowired
    TCommentService tCommentService;

    @Autowired
    MaterService materService;

    @Value("${server.port}")
    private String port;

    //商家回复评论
    @PostMapping("/addComment")
    public JSONObject addComment(@RequestParam("cid")Integer cid,@RequestParam("textManage")String textManage){
        JSONObject result = new JSONObject();
        List<TComment> list = tCommentService.list();
        //遍历查询结果，对比传入的评论ID
        for (TComment tc : list){
            String commentManage = tc.getTextManage();
            Integer commentCid = tc.getCid();
            if (cid.equals(commentCid)){
                if (StringTools.isNullOrEmpty(commentManage)){
                    TComment tComment = new TComment(cid,textManage);
                    tCommentService.updateById(tComment);
                    result.put("result","评论成功");
                }else {
                    result.put("result","管理员评论过了");
                }
            }
        }
        return result;
    }

    @GetMapping("/selectTComment")
    public JSONObject selectTComment(@RequestBody JSONObject jsonObject){
        Integer gid = jsonObject.getInteger("gid");
        Integer pagesNumber = jsonObject.getInteger("pagesNumber");
        //分页页码默认显示1
        if (StringTools.isNullOrEmpty(pagesNumber)){
            pagesNumber = 1;
        }else if (pagesNumber <= 0){
            pagesNumber = 1;
        }
        QueryWrapper<Mater> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Mater> select = queryWrapper.eq("gid", gid).select("pid");
        List<Mater> list = materService.list(select);
        List<TComment> tCommentList = new ArrayList<>();
        for (Mater m : list){
            int pid = m.getPid();
            QueryWrapper<TComment> queryWrapper1 = new QueryWrapper<>();
            QueryWrapper<TComment> tComment = queryWrapper1.eq("pid", pid).orderByDesc("create_time");
            List<TComment> list1 = tCommentService.list(tComment);
            for (TComment tc : list1){
                String image = tc.getImage();
                if (StringTools.isNullOrEmpty(image)){
                    tc.setImage(null);
                    tCommentList.add(tc);
                }else {
                    String imageAddr = ImageUtil.getImageAddr(image, "9002");
                    tc.setImage(imageAddr);
                    tCommentList.add(tc);
                }
            }
        }
        Map<String, Object> map = ListPagesUitl.listPaging(tCommentList, pagesNumber, 10);
        return CommonUtil.successJson(map);
    }

    //删除评论
    @PostMapping("/deleteTComment")
    public JSONObject deleteTComment(@RequestBody JSONObject jsonObject){
        JSONObject result = new JSONObject();
        Integer cid = jsonObject.getInteger("cid");
        List<Integer> cidList = new ArrayList<>();
        if (StringTools.isNullOrEmpty(cid)){
            result.put("Error","传入的评论id为空");
        }else {
            QueryWrapper<TComment> queryWrapper = new QueryWrapper<>();
            QueryWrapper<TComment> select = queryWrapper.select("cid");
            List<TComment> list = tCommentService.list(select);
            for (TComment tc : list){
                cidList.add(tc.getCid());
            }
            if (cidList.contains(cid)){
                tCommentService.removeById(cid);
                result.put("result",true);
            }else {
                result.put("Error","传入的评论id不存在");
            }
        }
        return result;
    }
}

